require "intensifyCfg"

local uf_bg = UIInfo.create()
local uf_icon = UIInfo.create()
local uf_coin = UIInfo.create()
local uf_coin_fnt = UIInfo.create()
local uf_desc = UIInfo.create()
local uf_ok = UIInfo.create()
local uf_lvbg = UIInfo.create()
local uf_lv1 = UIInfo.create()
local uf_lv2 = UIInfo.create()
local uf_lv3 = UIInfo.create()
local uf_lv4 = UIInfo.create()
local uf_recommend = UIInfo.create()

local coinFnt = pp.fnt_white_pty


IntensifyPanel = class("IntensifyPanel")

function IntensifyPanel.create(parent)
    local panel = IntensifyPanel.new()
    panel:init(parent)

    panel.view:setPosition(pp.center.x, pp.center.y)
    uiEffect.bounceIn(panel.view)    
    parent:base_onOpenWin(panel)
    
    return panel
end

function IntensifyPanel:init(parent)
    self.view = ccs.GUIReader:getInstance():widgetFromJsonFile("qianghuaPanel.json")
    parent.widget:addChild(self.view)
    self.parent = parent
    self.view:setLocalZOrder(pp.z_intensify)

    local function btCloseCallback(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn2)

        elseif eventType == ccui.TouchEventType.ended then
            
            uiEffect.bounceOut(self.view, true)
            self.parent:base_onCloseWin(self)
         end
    end
    local bt_close = self.view:getChildByName("_close")
    bt_close:addTouchEventListener(btCloseCallback)
    
    -- local function btToLevelCallback(sender, eventType)
    --     if eventType == ccui.TouchEventType.ended then
    --         gameUtil.changeScene("level")
    --     end
    -- end
    -- local bt_continue = self.view:getChildByName("_to_level")
    -- bt_continue:addTouchEventListener(btToLevelCallback)

    self.list = self.view:getChildByName("_list")
    local protoItem = self.list:getChildByName("bg1")
    uf_bg:readInfoFromNode(protoItem)

    do
        local ui = protoItem:getChildByName("_icon")
        uf_icon:readInfoFromNode(ui)
    end
    
    do
        local ok = protoItem:getChildByName("_ok")
        uf_ok:readInfoFromNode(ok)

        do
            local ui = ok:getChildByName("_coin")
            uf_coin:readInfoFromNode(ui)
        end
        do
            local ui = ok:getChildByName("_coin_fnt")
            uf_coin_fnt:readInfoFromNode(ui)
        end
    end
    do
        local ui = protoItem:getChildByName("_desc")
        uf_desc:readInfoFromNode(ui)
    end
    do
        local ui = protoItem:getChildByName("lv_bg")
        uf_lvbg:readInfoFromNode(ui)
    end
    do
        local ui = protoItem:getChildByName("_lv1")
        uf_lv1:readInfoFromNode(ui)
    end

    do
        local ui = protoItem:getChildByName("_lv2")
        uf_lv2:readInfoFromNode(ui)
    end

    do
        local ui = protoItem:getChildByName("_lv3")
        uf_lv3:readInfoFromNode(ui)
    end

    do
        local ui = protoItem:getChildByName("_lv4")
        uf_lv4:readInfoFromNode(ui)
    end

    do
        local ui = protoItem:getChildByName("_recommend")
        uf_recommend:readInfoFromNode(ui)
    end
    
    protoItem:removeFromParent(true)
    ------------------------------------------------

    local btCallbacks = {}

      local function callback1(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn1)

        elseif eventType == ccui.TouchEventType.ended then
            local needcoin = intensifyCfg[1].coin[userData.weaponLv_colorful]
            if userData.coin >= needcoin then
                userData.coin = userData.coin - needcoin
                userData.weaponLv_colorful = userData.weaponLv_colorful + 1
                self:refreshByUserData()
                gameUtil.flushUserRecord()
                uiPopNotify:popString(gameUtil.curLuaScene.widget, "强化成功")
                AudioEngine.playEffect(sound.fuhuo)

                gameUtil.sendGameStat(et.GL_Qianghua1, userData.weaponLv_colorful)  --stat
            else
                uiPopNotify:popString(gameUtil.curLuaScene.widget, "金币不足")
                if DEF_CHN_XNY then
                    pchCenter:order(pchCfg.item.JinBiLiBao)
                else
                    WarningPanel.create(self.parent, "Pay_level_Libao_Jinbi")
                end
            end
        end
    end
    btCallbacks[#btCallbacks+1] = callback1

    local function callback2(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn1)

        elseif eventType == ccui.TouchEventType.ended then
            local needcoin = intensifyCfg[1].coin[userData.weaponLv_boom]
            if userData.coin >= needcoin then
                userData.coin = userData.coin - needcoin
                userData.weaponLv_boom = userData.weaponLv_boom + 1
                self:refreshByUserData()
                gameUtil.flushUserRecord()
                uiPopNotify:popString(gameUtil.curLuaScene.widget, "强化成功")
                AudioEngine.playEffect(sound.fuhuo)

                gameUtil.sendGameStat(et.GL_Qianghua2, userData.weaponLv_boom)
            else
                uiPopNotify:popString(gameUtil.curLuaScene.widget, "金币不足")
                if DEF_CHN_XNY then
                    pchCenter:order(pchCfg.item.JinBiLiBao)
                else
                    WarningPanel.create(self.parent, "Pay_level_Libao_Jinbi")
                end
            end
        end
    end
    btCallbacks[#btCallbacks+1] = callback2

    local function callback3(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn1)

        elseif eventType == ccui.TouchEventType.ended then
            local needcoin = intensifyCfg[1].coin[userData.weaponLv_huanman]
            if userData.coin >= needcoin then
                userData.coin = userData.coin - needcoin
                userData.weaponLv_huanman = userData.weaponLv_huanman + 1
                self:refreshByUserData()
                gameUtil.flushUserRecord()
                uiPopNotify:popString(gameUtil.curLuaScene.widget, "强化成功")
                AudioEngine.playEffect(sound.fuhuo)

                gameUtil.sendGameStat(et.GL_Qianghua3, userData.weaponLv_huanman)
            else
                uiPopNotify:popString(gameUtil.curLuaScene.widget, "金币不足")
                if DEF_CHN_XNY then
                    pchCenter:order(pchCfg.item.JinBiLiBao)
                else
                    WarningPanel.create(self.parent, "Pay_level_Libao_Jinbi")
                end
            end
        end
    end
    btCallbacks[#btCallbacks+1] = callback3

       
    local function callback4(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn1)

        elseif eventType == ccui.TouchEventType.ended then
            local needcoin = intensifyCfg[1].coin[userData.weaponLv_fenji]
            if userData.coin >= needcoin then
                userData.coin = userData.coin - needcoin
                userData.weaponLv_fenji = userData.weaponLv_fenji + 1
                self:refreshByUserData()
                gameUtil.flushUserRecord()
                uiPopNotify:popString(gameUtil.curLuaScene.widget, "强化成功")
                AudioEngine.playEffect(sound.fuhuo)

                gameUtil.sendGameStat(et.GL_Qianghua4, userData.weaponLv_fenji)
            else
                uiPopNotify:popString(gameUtil.curLuaScene.widget, "金币不足")
                if DEF_CHN_XNY then
                    pchCenter:order(pchCfg.item.JinBiLiBao)
                else
                    WarningPanel.create(self.parent, "Pay_level_Libao_Jinbi")
                end
            end
        end
    end
    btCallbacks[#btCallbacks+1] = callback4

    local function callback5(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn1)

        elseif eventType == ccui.TouchEventType.ended then
            local needcoin = intensifyCfg[1].coin[userData.weaponLv_yuheng]
            if userData.coin >= needcoin then
                userData.coin = userData.coin - needcoin
                userData.weaponLv_yuheng = userData.weaponLv_yuheng + 1
                self:refreshByUserData()
                gameUtil.flushUserRecord()
                uiPopNotify:popString(gameUtil.curLuaScene.widget, "强化成功")
                AudioEngine.playEffect(sound.fuhuo)

                gameUtil.sendGameStat(et.GL_Qianghua5, userData.weaponLv_yuheng)
            else
                uiPopNotify:popString(gameUtil.curLuaScene.widget, "金币不足")
                if DEF_CHN_XNY then
                    pchCenter:order(pchCfg.item.JinBiLiBao)
                else
                    WarningPanel.create(self.parent, "Pay_level_Libao_Jinbi")
                end
            end
        end
    end
    btCallbacks[#btCallbacks+1] = callback5


    ------------------------------------------------

    local uf_lvs = {
        uf_lv1,uf_lv2,uf_lv3,uf_lv4
    }

    self.itemInfo = {}

    

    for k,v in ipairs(intensifyCfg) do
        --
        self.itemInfo[#self.itemInfo+1] = {
            lvImage = {},
            coin = nil,
            bt = nil
        }

        local bg = ccui.ImageView:create("common/di1.png", ccui.TextureResType.localType)
        uf_bg:setInfoToNode(bg)

        local icon_bg = ccui.ImageView:create("common/daojudi.png", ccui.TextureResType.localType)
        uf_icon:setInfoToNode(icon_bg)
        bg:addChild(icon_bg)

        local icon = ccui.ImageView:create(v.icon, ccui.TextureResType.localType)
        uf_icon:setInfoToNode(icon)
        bg:addChild(icon)

        local lvbg = ccui.ImageView:create("common/jibiedi.png", ccui.TextureResType.localType)
        uf_lvbg:setInfoToNode(lvbg)
        bg:addChild(lvbg)

        for i = 1, 4 do
            local lv = ccui.ImageView:create("common/jibie.png", ccui.TextureResType.localType)
            uf_lvs[i]:setInfoToNode(lv)
            bg:addChild(lv)
            local images = self.itemInfo[k].lvImage
            images[#images+1] = lv
        end 

        --recommend
        if v.bRecommend then
            local rec = ccui.ImageView:create("common/srcres/jian.png", ccui.TextureResType.localType)
            uf_recommend:setInfoToNode(rec)
            uiEffect.blink(rec)
            bg:addChild(rec)
        end

        local bt = ccui.Button:create("common/shengji.png")
        uf_ok:setInfoToNode(bt)
        bg:addChild(bt)
        self.itemInfo[k].bt = bt
        bt:addTouchEventListener(btCallbacks[k])


        local coinfnt = cc.LabelBMFont:create(v.coin[k],  pp.fnt_number_yellow)
        uf_coin_fnt:setInfoToNode(coinfnt)
        coinfnt:setScale(0.45)  
        bt:addChild(coinfnt)
        self.itemInfo[k].coin = coinfnt

        local coin = ccui.ImageView:create("common/jinbi.png", ccui.TextureResType.localType)
        uf_coin:setInfoToNode(coin)
        bt:addChild(coin)
        

        --fnt
        uiBMFont:setAddInfo(bg, 10)
        uiBMFont:setColor(cc.c3b(205,233,255))
        uiBMFont:createMultiLines( v.desc, pp.fnt_qianghua, 0.75, 200, uf_desc.position.x, uf_desc.position.y, 22)

        self.list:addChild(bg)
    end

    self:refreshByUserData()
end 


function IntensifyPanel:refreshByUserData()
    local fenjiUiLv = userData.weaponLv_fenji - 1
    local yuhengUiLv = userData.weaponLv_yuheng - 1
    local huanmanUiLv = userData.weaponLv_huanman -1
    local boomUiLv = userData.weaponLv_boom - 1
    local colorfulUiLv = userData.weaponLv_colorful - 1

    local lvs = {colorfulUiLv, boomUiLv, huanmanUiLv, fenjiUiLv, yuhengUiLv}

    for k,v in ipairs(lvs)do
        local uiholder = self.itemInfo[k]

        for i = 1,4 do
            if v >= i then
                uiholder.lvImage[i]:setVisible(true)
            else
                uiholder.lvImage[i]:setVisible(false)
            end
        end
        
        if v >= intensifyCfg.lvMax then
            uiholder.bt:setVisible(false)
            uiholder.coin:setVisible(false)

        else
            uiholder.bt:setVisible(true)
            uiholder.coin:setVisible(true)
            uiholder.coin:setString(string.format("%d",intensifyCfg[k].coin[v+1]))

        end
        
        if k > 3 or v > 4 then
            d_print("")
        end
   

        
    end

    self.parent:refreshCoin(userData.coin)
end

